/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       dialog_resurrect.h

	$Header: /game/dialog_resurrect.h $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "dialog_resurrect.h"

#include "bitmap_group.h"
#include "bitmap_group_cache.h"
#include "bitmap_layer_window.h"
#include "bound_handler.h"
#include "button.h"
#include "button_cache.h"
#include "creature_select_window.h"
#include "external_string.h"
#include "format_string.h"
#include "game_replace_keywords.h"
#include "hero.h"
#include "player.h"
#include "random.h"
#include "standard_fonts.h"
#include "text_window.h"


extern t_external_string const		k_hotkey_buy;
extern t_button_cache const			k_buy_button;
extern t_button_cache const			k_close_button;
extern t_bitmap_group_cache const   k_large_material_bitmaps;
static t_bitmap_group_cache const   k_layout( "dialog.resurrect" );
static t_external_string const      k_text_instructions( "resurrect_instructions.misc" );

// -------------------------------------------------------------------
// dialog to choose resurrection targets
// -------------------------------------------------------------------
t_dialog_resurrect::t_dialog_resurrect( t_window* parent, std::vector<t_hero*>& heroes, 
									    t_player* player )
				  : t_window( k_completely_transparent, parent )
{
	t_screen_rect rect;

	m_player = player;
	m_bitmaps = k_layout.get();
	m_material_bitmaps = k_large_material_bitmaps.get();
	rect = m_bitmaps->get_rect();
	rect += (parent->get_client_rect().size() - rect.size()) / 2;
	init( rect );

	// add background
	t_bitmap_layer const* layer;
	t_window*             background;
	t_window*			  window;
	t_screen_point		  origin(0,0);

	layer = m_bitmaps->find( "top" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_bitmaps->find( "left" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_bitmaps->find( "right" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_bitmaps->find( "bottom" );
	window = new t_bitmap_layer_window( layer, origin, this );
	layer = m_bitmaps->find( "background" );
	background = new t_bitmap_layer_window( layer, origin, this );
	origin = layer->get_rect().top_left();

	// add label
	t_text_window* text_window;

	rect = m_bitmaps->find( "text_dialog" )->get_rect() - origin;
	text_window = new t_text_window( get_font( 20 ), rect, background,
		                             k_text_instructions, t_pixel_24(0,0,0) );
	text_window->set_drop_shadow( false );
	text_window->set_center_vertical();
	text_window->set_center_horizontal();

	// add close button
	t_button_handler handler;
	t_button*		 button;

	rect = m_bitmaps->find( "close_button" )->get_rect() - origin;
	handler = bound_handler( *this, &t_dialog_resurrect::close_clicked );
	button = new t_button( k_close_button.get(), rect.top_left(), background );
	button->set_click_handler( handler );

	// add purchase button
	rect = m_bitmaps->find( "purchase_icon" )->get_rect() - origin;
	handler = bound_handler( *this, &t_dialog_resurrect::buy_clicked );
	m_buy_button = new t_button( k_buy_button.get(), rect.top_left(), background );
	m_buy_button->set_click_handler( handler );

	// add gold icon
	rect = m_bitmaps->find( "gold_icon" )->get_rect() - origin;
	layer = m_material_bitmaps->find( "gold" );
	window = new t_bitmap_layer_window( layer, rect.top_left(), background );

	// add gold text
	rect = m_bitmaps->find( "text_gold" )->get_rect() - origin;
	m_cost_window = new t_text_window( get_font( rect.height() ), rect, background,
		                               "0", t_pixel_24(0,0,0) );
	m_cost_window->set_drop_shadow( false );
	m_cost_window->set_center_horizontal();

	// Create hero selection portraits. 
	t_screen_point point;
	int			   i;

	m_current_cost = 0;
	point = m_bitmaps->find("hero_portraits")->get_rect().top_left() - origin;
	m_hero_selection_window = new t_creature_select_window( point, false, background );
	for (i = 0; i < heroes.size(); i++)
	{
		m_hero_selection_window->add( heroes[i] );
	    m_hero_selection_window->set_help_text( i, heroes[i]->get_name() );
	}
	m_hero_selection_window->set_handler( bound_handler( *this, 
		                                  &t_dialog_resurrect::selection_change ));
	m_hero_selection_window->select_all();
}

// -------------------------------------------------------------------
// dialog to choose resurrection targets
// -------------------------------------------------------------------
void t_dialog_resurrect::buy_clicked( t_button* button )
{
	int     i;
	t_hero* hero;

	m_player->spend( k_gold, m_current_cost );
	for (i = 0; i < m_hero_selection_window->get_count(); i++)
	{
		if (!m_hero_selection_window->is_selected(i))
			continue;
		hero = m_hero_selection_window->get_creature(i)->get_hero();
		hero->set_dead( false );
		hero->set_wounds( 0 );
	}
	close();
}

// -------------------------------------------------------------------
// dialog to choose resurrection targets
// -------------------------------------------------------------------
void t_dialog_resurrect::close_clicked( t_button* button )
{
	close();
}

// -------------------------------------------------------------------
// dialog to choose resurrection targets
// -------------------------------------------------------------------
void t_dialog_resurrect::selection_change( t_creature_select_window*, 
										   t_creature_stack* )
{
	int     i;
	t_hero* hero;

	m_current_cost = 0;
	for (i = 0; i < m_hero_selection_window->get_count(); i++)
	{
		if (!m_hero_selection_window->is_selected(i))
			continue;

		hero = m_hero_selection_window->get_creature(i)->get_hero();
		m_current_cost += hero->get_resurrection_cost();
	}
	m_cost_window->set_text( format_string( "%i", m_current_cost ) );
	if (m_current_cost > m_player->get_funds()[k_gold]
		|| m_current_cost == 0)
		m_buy_button->enable( false );
	else
		m_buy_button->enable( true );
}
